package com.icss.ah.promotion.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.icss.ah.base.action.BaseAction;
import com.icss.ah.base.bean.EasyuiDataGridJson;
import com.icss.ah.base.bean.Json;
import com.icss.ah.login.bean.UserInfoBean;
import com.icss.ah.productForSale.service.ProductForSaleService;
import com.icss.ah.promotion.service.PromotionService;
import com.icss.j2ee.util.UUID;

/**
 * 
 * @author
 *
 */
@Controller
@Scope("prototype")
public class PromotionAction extends BaseAction {
	
	private static final long serialVersionUID = 112321L;
	
	@Autowired
	private PromotionService promotionService;
	@Autowired
	private ProductForSaleService productForSaleService;
	private String flag;
	
	public String toPromotionListPage(){
		String returnPage = "toPromotionListPage";

		
		UserInfoBean userInfoBean= this.getUserInfo(getUserCodeFromSession());
		String shopId = userInfoBean.getShopId();
		String corpId = userInfoBean.getCorpId();
		
		request = this.getRequest();
		HashMap map  = new HashMap();
		map.put("shopId", shopId);
		map.put("corpId", corpId);
		//List yjfl =  productForSaleService.getProductType1List("ProductForSaleBean.getProductType1List", map);
		
	//	request.setAttribute("yjfl", yjfl);
		return returnPage;
	}
	
	public String toPromotionPage(){
		try {
			String returnPage = "toPromotionPage";
	
			
			UserInfoBean userInfoBean= this.getUserInfo(getUserCodeFromSession());
			String shopId = userInfoBean.getShopId();
			String corpId = userInfoBean.getCorpId();
			
			request = this.getRequest();
			HashMap map  = new HashMap();
			map.put("shopId", shopId);
			map.put("corpId", corpId);
			List yjfl =  productForSaleService.getProductType1List("ProductForSaleBean.getProductType1List", map);
			request.setAttribute("yjfl", yjfl);
			
			String promotionId  = request.getParameter("promotionId")==null?"":request.getParameter("promotionId").trim();
			Map promotion  = new HashMap();
			if(!"".equals(promotionId)){
				HashMap query  = new HashMap();
				query.put("promotionId", promotionId);
				promotion  = promotionService.getPromotionDetail("PromotionBean.getPromotionDetail", query);
			}
			request.setAttribute("promotionId", promotionId);
			request.setAttribute("promotion", promotion);
			request.setAttribute("flag", flag);
			
			return returnPage;
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return "toPromotionPage";
	}
	
	
	public void productList(){
		request = this.getRequest();
		

		UserInfoBean userInfoBean= this.getUserInfo(getUserCodeFromSession());
		String shopId = userInfoBean.getShopId();
		String corpId = userInfoBean.getCorpId();
		
		String title  = request.getParameter("title").trim();
		if(title==null||"".equals(title)){
			title = "";
		}
		String minPriceStr  = request.getParameter("minPriceStr")==null?"":request.getParameter("minPriceStr").trim();
		String maxPriceStr  = request.getParameter("maxPriceStr")==null?"":request.getParameter("maxPriceStr").trim();
		
		String firstSeries  = request.getParameter("firstSeries")==null?"0":request.getParameter("firstSeries").trim();
		String secondSeries  = request.getParameter("secondSeries")==null?"0":request.getParameter("secondSeries").trim();
		String serise ="";
		if(secondSeries==null||"".equals(secondSeries)||"0".equals(secondSeries)||"null".equals(secondSeries)){
			if(firstSeries!=null&&!"null".equals(firstSeries)&&!"0".equals(firstSeries)) 
			serise = firstSeries==null?"":firstSeries;
		}else{
			 serise = secondSeries;
		}
		String promotionId  = request.getParameter("promotionId")==null?"":request.getParameter("promotionId").trim();
		String result = "";
		try {
			dataGridJson = new EasyuiDataGridJson();
			HashMap map = new HashMap();
			map.put("corpId",corpId);	//公众号id
			map.put("shopId", shopId);	//商铺id
			map.put("title", title);
			map.put("minPriceStr", minPriceStr);
			map.put("maxPriceStr", maxPriceStr);
			map.put("serise",serise);
			map.put("promotionId",promotionId);
			List list =  promotionService.getProductList("PromotionBean.getProductList", map);
			result = JSONArray.fromObject(list).toString();
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			str2Resp(result);
		}
	}
	
	
	public void promotionList(){
		request = this.getRequest();
		
		UserInfoBean userInfoBean= this.getUserInfo(getUserCodeFromSession());
		String shopId = userInfoBean.getShopId();
		String corpId = userInfoBean.getCorpId();
		
		String name  = request.getParameter("name")==null?"0":request.getParameter("name").trim();
		String date  = request.getParameter("date")==null?"0":request.getParameter("date").trim();
		
		String result = "";
		try {
			dataGridJson = new EasyuiDataGridJson();
			HashMap map = new HashMap();
			map.put("corpId",corpId);	//公众号id
			map.put("shopId", shopId);	//商铺id
			map.put("date", date);
			map.put("name", name);
			map.put("startNo",Integer.valueOf(getStartIndexMysql()));
			map.put("pageRows",Integer.valueOf(getEndIndexMysql()));
			
			List<Map> productList = new ArrayList();
			productList = promotionService.getPromotionList("PromotionBean.getPromotionList", map);
			long productCount = promotionService.getPromotionListCount("PromotionBean.getPromotionListCount", map);//查询总记录数

			dataGridJson.setRows(productList);
			dataGridJson.setTotal(productCount);
			result = JSONObject.fromObject(dataGridJson).toString();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			str2Resp(result);
		}
	}
	
	
	public void add(){
		try {
			jsonText = new Json();
			request = this.getRequest();
			
			UserInfoBean userInfoBean= this.getUserInfo(getUserCodeFromSession());
			String shopId = userInfoBean.getShopId();
			String corpId = userInfoBean.getCorpId();
			
			
			String promotionName  = request.getParameter("promotionName")==null?"":request.getParameter("promotionName").trim();
			String startDate  = request.getParameter("startDate")==null?"":request.getParameter("startDate").trim();
			String endDate  = request.getParameter("endDate")==null?"":request.getParameter("endDate").trim();
			String fanwei  = request.getParameter("fanwei")==null?"":request.getParameter("fanwei").trim();
			String leixing  = request.getParameter("leixing")==null?"":request.getParameter("leixing").trim();
			String zhekou  = request.getParameter("zhekou")==null?"":request.getParameter("zhekou").trim();
			String xiangou  = request.getParameter("xiangou")==null?"":request.getParameter("xiangou").trim();
			String status  = request.getParameter("status")==null?"":request.getParameter("status").trim();
			
			String id = new UUID().toString();
			HashMap map  = new HashMap();
			map.put("uuid", id);
			map.put("promotionName", promotionName);
			map.put("startDate", startDate);
			map.put("endDate", endDate);
			map.put("fanwei", fanwei);
			map.put("leixing", leixing);
			map.put("zhekou",zhekou);
			map.put("xiangou",xiangou);
			map.put("status", status);
			map.put("corpId", corpId);
			map.put("shopId", shopId);
			
			promotionService.add("PromotionBean.add", map);
			
			String productIds = request.getParameter("productIds");
			String[] ids = productIds.split(",");
			for(int i=0;i<ids.length;i++){
				if(!"".equals(ids[i])){
					HashMap m  = new HashMap();
					m.put("uuid", new UUID().toString());
					m.put("pid", id);
					m.put("productId", ids[i]);
					m.put("corpId", corpId);
					m.put("shopId", shopId);
					promotionService.addProduct("PromotionBean.addProduct", m);
				}
				
			}
			jsonText.setSuccess(true);
			jsonText.setObj(id);
		} catch (Exception e) {
			e.printStackTrace();
			jsonText.setSuccess(false);
		} finally {
			str2Resp(JSONObject.fromObject(jsonText).toString());
		}
	}
	
	public void edit(){
		try{
			jsonText = new Json();
			request = this.getRequest();

			UserInfoBean userInfoBean= this.getUserInfo(getUserCodeFromSession());
			String shopId = userInfoBean.getShopId();
			String corpId = userInfoBean.getCorpId();
			
			String promotionId  = request.getParameter("promotionId")==null?"":request.getParameter("promotionId").trim();
			String promotionName  = request.getParameter("promotionName")==null?"":request.getParameter("promotionName").trim();
			String startDate  = request.getParameter("startDate")==null?"":request.getParameter("startDate").trim();
			String endDate  = request.getParameter("endDate")==null?"":request.getParameter("endDate").trim();
			String fanwei  = request.getParameter("fanwei")==null?"":request.getParameter("fanwei").trim();
			String leixing  = request.getParameter("leixing")==null?"":request.getParameter("leixing").trim();
			String zhekou  = request.getParameter("zhekou")==null?"":request.getParameter("zhekou").trim();
			String xiangou  = request.getParameter("xiangou")==null?"":request.getParameter("xiangou").trim();
			String status  = request.getParameter("status")==null?"":request.getParameter("status").trim();
			
		
			HashMap map  = new HashMap();
			map.put("promotionId", promotionId);
			map.put("promotionName", promotionName);
			map.put("startDate", startDate);
			map.put("endDate", endDate);
			map.put("fanwei", fanwei);
			map.put("leixing", leixing);
			map.put("zhekou",zhekou);
			map.put("xiangou",xiangou);
			map.put("status", status);
			map.put("shopId", shopId);
			map.put("corpId", corpId);
			
			
			promotionService.edit("PromotionBean.edit", map);
			promotionService.delProduct("PromotionBean.delProduct", map);
			
			String productIds = request.getParameter("productIds");
			String[] ids = productIds.split(",");
			for(int i=0;i<ids.length;i++){
				if(!"".equals(ids[i])){
					HashMap m  = new HashMap();
					m.put("uuid", new UUID().toString());
					m.put("pid", promotionId);
					m.put("productId", ids[i]);
					m.put("corpId", corpId);
					m.put("shopId", shopId);
					promotionService.addProduct("PromotionBean.addProduct", m);
				}
				
			}
			jsonText.setSuccess(true);
		}catch(Exception e){
			e.printStackTrace();
			jsonText.setSuccess(false);
		}finally{
			str2Resp(JSONObject.fromObject(jsonText).toString());
		}
	}
	
	public void del(){
		try {
			jsonText = new Json();
			request = this.getRequest();
			
			UserInfoBean userInfoBean= this.getUserInfo(getUserCodeFromSession());
			String shopId = userInfoBean.getShopId();
			String corpId = userInfoBean.getCorpId();
			String promotionId  = request.getParameter("promotionId")==null?"":request.getParameter("promotionId").trim();
			
			HashMap map  = new HashMap();
			map.put("promotionId", promotionId);
			map.put("shopId", shopId);
			map.put("corpId", corpId);
			promotionService.delProduct("PromotionBean.delProduct", map);
			promotionService.del("PromotionBean.del", map);
			
			jsonText.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			jsonText.setSuccess(false);
		} finally {
			str2Resp(JSONObject.fromObject(jsonText).toString());
		}
	}
	/**
	 * 更改启用/停用状态
	 */
	public void updateStatus(){
		jsonText = new Json();
		try {
			request = this.getRequest();
			
			UserInfoBean userInfoBean= this.getUserInfo(getUserCodeFromSession());
			String shopId = userInfoBean.getShopId();
			String corpId = userInfoBean.getCorpId();
			String promotionId  = request.getParameter("promotionId")==null?"":request.getParameter("promotionId").trim();
			String status  = request.getParameter("status")==null?"":request.getParameter("status").trim();
			
			HashMap map  = new HashMap();
			map.put("promotionId", promotionId);
			map.put("shopId", shopId);
			map.put("corpId", corpId);
			map.put("status", status);
			
			boolean result = promotionService.updateStatus(map);
			jsonText.setSuccess(result);
		} catch (Exception e) {
			e.printStackTrace();
			jsonText.setSuccess(false);
		} finally {
			str2Resp(JSONObject.fromObject(jsonText).toString());
		}
	}
	
	public Json getJsonText() {
		return jsonText;
	}

	public void setJsonText(Json jsonText) {
		this.jsonText = jsonText;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}
	
}
